package impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import dao.BaseDao;
import dao.BooksDao;
import entity.Books;

public class BooksImpl extends BaseDao implements BooksDao{

	@Override
	public List<Books> allBooks() {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String sql="SELECT * FROM books";
		List<Books> books=new ArrayList<Books>();
		try {
			conn=getConnection();
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Books book=new Books();
				book.setBook_phone(rs.getString("book_phone"));
				book.setBook_imgurl(rs.getString("book_imgurl"));
				book.setBook_dateails(rs.getString("book_dateails"));
				book.setBook_date(rs.getString("book_date"));
				book.setBook_user_name(rs.getString("book_user_name"));
				book.setBook_number(rs.getString("book_number"));
				book.setId(rs.getString("id")); 
				books.add(book);
			}
		} catch (Exception e) {
		e.printStackTrace();
		}finally{
			closeAll(rs, pstmt, conn);
		}
		return books;
	}

	@Override
	public int addBooks(Books books) {
		String sql = "INSERT books (book_dateails,book_phone,id,book_date,book_number,book_user_name,book_imgurl) VALUES(?,?,?,?,?,?,?)";
		Object[] objects = {books.getBook_dateails(),books.getBook_phone(), books.getId(),books.getBook_date(),books.getBook_number(),books.getBook_user_name(),books.getBook_imgurl()};
		return execeteUpdata(sql, objects);
	}

	@Override
	public int updBooks(Books books) {
		String sql = "update books set book_imgurl=?,book_dateails=?,book_phone=?,book_date=?,book_number=?,book_user_name=? where id=?";
		Object[] objects = { books.getBook_imgurl(),books.getBook_dateails(),books.getBook_phone(),books.getBook_date(),books.getBook_number(),books.getBook_user_name(),books.getId()};
		return execeteUpdata(sql, objects);
	}

	@Override
	public int delBooks(Books books) {
		String sql = "delete from books where id=?";
		Object[] objects = { books.getId()};
		return execeteUpdata(sql, objects);
	}

}
